<?php

namespace App\Providers;

use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Prettus\Repository\Providers\RepositoryServiceProvider;
use Zhuzhichao\BankCardInfo\BankCard;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if (config('app.website.HTTPS') == true) {
            \URL::forceScheme('https');
        }
        Validator::extend('domain', function ($attribute, $value, $parameters, $validator) {
            $domains = explode(':', $value);
            if (filter_var($domains[0], FILTER_VALIDATE_IP)) {
                return true;
            }
            exec("ping -c 1 {$domains[0]}", $output, $status);
            if ($status == 0) {
                return true;
            }
            return false;
        });
        Validator::extend('bank_no', function ($attribute, $value, $parameters, $validator) {
            $bankCard = BankCard::info($value);
            if ($bankCard['validated']) {
//            if (luhmBank($value)) {
                return true;
            }
            return false;
        });
        Validator::extend('phone', function ($attribute, $value, $parameters, $validator) {
            if (preg_match('/^1[3|4|5|7|8][0-9]{9}$/', $value)) {
                return true;
            }
            return false;
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
        $this->app->register(RepositoryServiceProvider::class);
    }
}
